import Vue from 'vue';
import api from "@/service/api";
import http from "@/service/http";

//状态
const state={
	//用户列表
	tableData:[],
	//表格选择行数据
	selects:[],
	//总记录数
	totalNum:0,
	//当前页
	currentPage:1,
	//每页显示记录数
	pageSize:20,
	//表格加载状态
	listLoading:false,
	//过虑条件
	filters:{
		employeename:'',
		username:'',
		status:''
	}
}

//getter
const getters={
	tableData:state=>state.tableData,
	selects:state=>state.selects,
	pageSize:state=>state.pageSize,
	currentPage:state=>state.currentPage,
	totalNum:state=>state.totalNum,
	listLoading:state=>state.listLoading,
	filters:state=>state.filters
}

//actions
const actions={
	getRoles({commit,state}){
		let param={
			pageNum:state.currentPage,pageSize:state.pageSize
		}
		commit('listLoading',true);
		http.post(api.sysApi.roleList,param).then(data => {
			if(!data.success){
				return;
			}
            commit('loadList',data);
			commit('listLoading',false);
        }); 
	},
	//表格选择行
	handleSelectionChange({commit,dispatch},selects){
		commit('selectsChange',selects);
	},
	//表格翻页
	handleCurrentChange({commit,dispatch},currentPage){
		commit('currentChange',currentPage);
		dispatch('getRoles');
	},
	//表格设置每页条数
	handleSizeChange({commit,dispatch},pageSize){
		commit('sizeChange',pageSize);
		dispatch('getRoles');
	},
	//重置过虑器
	resetFilters({commit,dispatch},filters){
		commit('resetFilters',filters);
		dispatch('getRoles');
	},
	//表格载入状态
	listLoading({commit},isShow){
		commit('listLoading',isShow);
	}
}

//mutations
const mutations={
	//角色列表数据状态变更
	loadList(state,param){
		state.totalNum=param.totalNum;
		state.tableData=param.data;
	},
	//表格载入状态变更
	listLoading(state,isShow){
		state.listLoading=isShow;
	},
	//表格选中行数据状态变更
	selectsChange(state,selects){
		state.selects=selects;
	},
	//表格翻页状态变更
	currentChange(state,currentPage){
		state.currentPage=currentPage;
	},
	//表格每页显示条数状态变更
	sizeChange(state,pageSize){
		state.pageSize=pageSize;
	},
	//查询条件过虑器状态充更
	resetFilters(state,filters){
		state.filters=filters;
	}
}

export default{
	namespaced:true,
	state,
	getters,
	actions,
	mutations
}